package com.xwj.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xwj.back.mapper.FeedBackMapper;
import com.xwj.back.pojo.FeedBack;
import com.xwj.back.service.FeedBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FeedBackServiceImpl implements FeedBackService {

    @Autowired
    private FeedBackMapper feedBackMapper;

    @Override
    public List<FeedBack> getAllFeedBack(String role, String username) {
        if("ROLE_ADMIN".equals(role)){
            return feedBackMapper.selectList(null);
        }
        QueryWrapper<FeedBack> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return feedBackMapper.selectList(queryWrapper);
    }

    @Override
    public String DeleteFeedBack(Integer id) {
        feedBackMapper.deleteById(id);
        return "success";
    }

    @Override
    public FeedBack getFeedBackById(Integer id) {
        return feedBackMapper.selectById(id);
    }

    @Override
    public String addFeedBack(Map<String, String> data) {
        String username = data.get("username");
        String avatar = data.get("avatar");
        String keyword = data.get("keyword");
        String content = data.get("content");
        Date date = new Date();
        feedBackMapper.insert(new FeedBack(null, username, avatar,
                keyword, content, false, date, null));
        return "success";
    }

    @Override
    public String changeStatus(Integer id, boolean status, String role) {
        if(!"ROLE_ADMIN".equals(role)) {
            return "error";
        }
        UpdateWrapper<FeedBack> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status",status).set("process_time", new Date()).eq("id",id);
        feedBackMapper.update(null,updateWrapper);
        return "success";
    }

    @Override
    public List<FeedBack> getByStatus(Map<String, String> data) {
        String role = data.get("role");
        String username = data.get("username");
        boolean status = Boolean.parseBoolean(data.get("status"));
        if("ROLE_ADMIN".equals(role)){
            QueryWrapper<FeedBack> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",status);
            return feedBackMapper.selectList(queryWrapper);
        }
        QueryWrapper<FeedBack> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("status", status);
        return feedBackMapper.selectList(queryWrapper);
    }

}
